cmake_minimum_required(VERSION 3.15)
project(detect_perf)

set(CMAKE_CXX_STANDARD 17)

option(USE_ONNXRUNTIME "Use onnxruntime" OFF)
option(USE_TOPSINFERENCE "Use TopsInference" ON)
option(USE_TOPS_FFMPEG "use Tops ffmpeg" ON)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -o3")
#set(ENV{HOME} /home/yuan)

if (USE_ONNXRUNTIME)
    set(ONNXRUNTIME_INCLUDE_DIR "$ENV{HOME}/onnxruntime-linux-x64-1.22.0/include")
    set(ONNXRUNTIME_LIB_DIR "$ENV{HOME}/onnxruntime-linux-x64-1.22.0/lib")
    set(ONNXRUNTIME_LIB onnxruntime)
    add_definitions(-DHAVE_ONNXRUNTIME=1)
endif()

if (NOT USE_TOPS_FFMPEG)
    # Find FFmpeg using pkg-config
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET
            libavcodec
            libavformat
            libavfilter
            libavutil
            libswscale
            libavdevice
    )

    set(FFMPEG_LINK_LIBS PkgConfig::FFMPEG)

else()
    set(FFMPEG_INCLUDE_DIRS $ENV{HOME}/buildroot/include)
    set(FFMPEG_LINK_DIRS /opt/tops/lib $ENV{HOME}/enrigin_sdk/opt/tops/lib)
    set(FFMPEG_LINK_LIBS avfilter avdevice avformat avcodec swresample swscale avutil)
endif()

if (USE_TOPSINFERENCE)
    find_path(TOPSINFERENCE_INCLUDE_DIR TopsInference/TopsInferRuntime.h
            PATHS /opt/tops/include $ENV{HOME}/enrigin_sdk/usr/include
            DOC "TopsInference include directory")

    find_path(TOPS_INCLUDE_DIR tops/tops_ext.h
            PATHS /opt/tops/include $ENV{HOME}/enrigin_sdk/opt/tops/include
            DOC "TopsInference include directory")

    find_library(TOPS_LIBS TopsInference
            PATHS /lib /opt/tops/lib $ENV{HOME}/enrigin_sdk/usr/lib
            DOC "TopsInference library")
endif()

#add_subdirectory(../otl otl.dir)

# 查找OpenCV
find_package(OpenCV REQUIRED)

include_directories(
        ${OpenCV_INCLUDE_DIRS}
        ${FFMPEG_INCLUDE_DIRS}
        ${ONNXRUNTIME_INCLUDE_DIR}
        ${TOPS_INCLUDE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}/../otl
        ${TOPSINFERENCE_INCLUDE_DIR}
        ${TOPS_INCLUDE_DIR}
)

link_directories(
        ${FFMPEG_LINK_DIRS}
        ${ONNXRUNTIME_LIB_DIR}
        ${TOPS_LIB_DIR}
)

set(DETECT_PERF_SRC_FILES
        src/main.cpp
)

if (USE_ONNXRUNTIME)
    list(APPEND DETECT_PERF_SRC_FILES
            src/detector/onnx_detector.cpp
            src/detector/yolo11_onnx_detector.cpp
    )
endif()

if (USE_TOPSINFERENCE)
    list(APPEND DETECT_PERF_SRC_FILES
            src/detector/yolo11_gcu_detector.cpp
    )
endif()

add_executable(detect_perf
        ${DETECT_PERF_SRC_FILES}
)

target_link_libraries(detect_perf
        ${OpenCV_LIBS}
        ${FFMPEG_LINK_LIBS}
        ${ONNXRUNTIME_LIB}
        ${TOPS_LIBS}
        otl
        pthread
)
